package com.sl.utils;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;

/**
 * 中值算法
 * @author yangdi
 *
 */
public class MeanGpsUtil {
	double startrange = 20; //初始波动范围
	double step = 10; //滤波递增范围
	double maxrange = 30; //最大波动范围
	int rangenum = 6; //滤波平差最小数目
	int outposrangecount = 5; //连续异常点范围
	
	int threshold = 5; //阈值
	
	int start = 0;
	int end = 0;
	
	int deleteCount = 0;
	
	ArrayList<LngLat> mLine = new ArrayList<LngLat>();
	
	public MeanGpsUtil(ArrayList<LngLat> mLine) {
		this.mLine = mLine;
	}
	
	private LngLat meangps(LngLat o, double startrange, double step, double maxrange,int rangenum) {
		LngLat result = null;
		if(startrange > maxrange) {
			return result;
		}
		int count = 0;
		double tempLat = 0;
		double tempLng = 0;
		ArrayList<Integer> tempPoint = new ArrayList<Integer>();
		ArrayList<Integer> tempPoint2 = new ArrayList<Integer>();
		
		boolean begin = false;
		boolean begin2 = false;
		for(int i=start; i< end;i++) {
			LngLat lngLat = mLine.get(i);
			double distance = AMapUtils.getDistance(o, lngLat);
			if(distance < this.startrange) {
				if(begin) {
					begin = false;
				}
				
				if(begin2) {
					begin2 = false;
					tempPoint.addAll(tempPoint2);
					tempPoint2.clear();
				}
				
				if (!begin && count == 0 && i > (start + end)/2) {
					begin = true;
				};
				if(!begin2 && count > 0  && i > (start + end)/2) {
					begin2 = true;
				}
				tempLat += lngLat.latitude;
				tempLng += lngLat.longitude;
				count++;
				
			}else {
				if(begin) {
					tempPoint.add(i);
				}
				if(begin2) {
					tempPoint2.add(i);
				}
			}
		}
		deleteCount = tempPoint.size();
		for(int j=tempPoint.size()-1;j>=0;j--) {
			mLine.remove(mLine.get((int)tempPoint.get(j)));
		}
		
		
//		if(count < rangenum) {
//			result = meangps(o, startrange + step, step, maxrange, rangenum);
//		}else {
//			tempLat = tempLat / count;
//			tempLng = tempLng / count;
//			result = new LngLat(tempLng, tempLat);
//		}
		
		//不设置阀值
		tempLat = tempLat / count;
		tempLng = tempLng / count;
		result = new LngLat(tempLng, tempLat);
		
		return result;
	}

	public ArrayList<LngLat> meangps(){
		ArrayList<LngLat> gLine = new ArrayList<LngLat>();
		int outposcount = 0;
		int count = mLine.size();
		boolean isLast = false;
		boolean isFirst = true;
		for(int i=0;i<count;i++) {
			int index = 0;
			if(isLast)
				break;
			if(i >= mLine.size())
				break;
			if(i - deleteCount > 15) {
				index = i - deleteCount;
			}else {
				index = 15;
			}
			
			this.start = index - 15;
			this.end = start + 30;
			if(index >=  mLine.size()) {
				index = 0;
			}
			
			if(this.end >= mLine.size()) {
				this.end = mLine.size();
				isLast = true;
			}
			
			LngLat lngLat = this.meangps(mLine.get(index), this.startrange, this.step, this.maxrange, this.rangenum);
			if(lngLat != null) {
				if(isFirst) {
					isFirst = false;
					gLine.add(lngLat);
					continue;
				}
				outposcount ++;
				if(outposcount > outposrangecount) {
					outposcount = 0;
				}else {
					gLine.add(lngLat);
				}
			}
		}
		return gLine;
	}

}
